package com.ztp.pc.gui.fileview;

import javax.swing.table.AbstractTableModel;

import com.ztp.lib.files.DirectoryChangeListener;
import com.ztp.lib.files.DisplayableExplorer;
import com.ztp.lib.files.DisplayableFile;

public class FileTableModel extends AbstractTableModel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8310379969599484730L;
	private final static String [] colNames = new String[]{"Name", "ext","Size","Last modified"};
	private DisplayableExplorer mExplorer;
	
	public FileTableModel(DisplayableExplorer de){
		if(de == null) throw new NullPointerException("ileTableModel(DirExplorer de): de == null!");
		mExplorer = de;
		mExplorer.addDirectoryChangeListener(new DirectoryChangeListener() {
			@Override
			public void directoryChanged( DisplayableFile currDir) {
				fireTableDataChanged();
			}

			@Override
			public void rootChanged(DisplayableFile root) {}
		});
	}
	/*
	public FileTableModel(File dirPath){
		this(new DirExplorer(dirPath));
	}
	
	public FileTableModel (String dirPath){
		this(new DirExplorer(dirPath));
	}
	*/

	@Override
	public int getColumnCount() {
		return 4;
	}

	@Override
	public int getRowCount() {
		return mExplorer.getSize();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		DisplayableFile file = mExplorer.getDisplayableFile(rowIndex);
		
		switch (columnIndex) {
			case 0:		return file;
			case 1:		return file.ext;
			case 2:		return file.size;
			case 3:		return file.modifyDafe;
	
			default:
				break;
		}
		return null;
	}

	@Override
	public String getColumnName(int column) {
		return colNames[column];
	}
	
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return getValueAt(0, columnIndex).getClass();
	}
	
}
